package xyz.hujf.otherbase.unsafe;

import org.junit.Test;
import sun.misc.Unsafe;

/**
 * @author Hujf
 * @title: TestUnsafe
 * @date 2021-04-2314:58
 * @description: TODO
 */
public class TestUnsafe {
    static final Unsafe usa = Unsafe.getUnsafe();

    static long stateOffset;

    private volatile long state = 0;

    static {
        try {
            stateOffset = usa.objectFieldOffset(TestUnsafe.class.getDeclaredField("state"));
        } catch (NoSuchFieldException e) {
            System.out.println("error");
            throw new Error(e);
        }
    }

    public static void main(String[] args) {

        TestUnsafe testUnsafe = new TestUnsafe();

        Boolean success =usa.compareAndSwapInt(testUnsafe,stateOffset,0,1);

        System.out.println(success);
    }
}
